import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import '../../../m_chatroom.dart';
import '../../interface/chat_room_leaf_interface.dart';
import 'dart:collection';

class RecommendRoomListPageModel extends ChatRoomChangeNotifier {
  RecommendRoomListPageModel(int curIndex) {
    currentTabIndex = curIndex;
    initData();
  }

  @override
  void dispose() {
    // CachedParkRoomData.instance.channelTagsData = channelTagsData;
    super.dispose();
  }

  initData() async {
    // var result = await RtcApi.instance.getChannelTags();
    // if(result!=null &&  result.data!=null){
    //   channelTagsData = result.data!;
    //   // int selectIndex = 0;
    //   // currentTabIndex = selectIndex;
    //   refreshItem(currentTabIndex,pageNum);
    // }
    notifyListeners();
  }

  // ChannelTagsData channelTagsData=ChannelTagsData.create();
  // Map<String,RtcSidebarRsp> sideBarMap = new HashMap();
  /// 列表数据 保存数据源 channelList 数据
  List itemList = [];
  // ItemScrollController scrollController = ItemScrollController();

  PageController controller = PageController();

  RefreshController refreshController =
      RefreshController(initialRefresh: false);

  bool canLoadMore = true;

  /// 当前的tab index
  int currentTabIndex = 0;
  int pageNum = 1;
  int pageSize = 10;
  String? snapshotId; // 快照id第一次请求不需要传

  bool isAnimating = false;

  // selectItem(int index) {
  //   if(currentTabIndex == 0 && index == 0){
  //     return;
  //   }
  //
  //   currentTabIndex = index;
  //   pageNum = 1;
  //   snapshotId = "";
  //   // itemList.clear();
  //
  //   isAnimating = true;
  //   refreshItem(index,pageNum);
  //   controller.animateToPage(currentTabIndex,
  //       duration: Duration(milliseconds: 0), curve: Curves.easeInOut);
  //   scrollController.scrollTo(index: index, duration: Duration(milliseconds: 0),alignment: 0.4);
  //   notifyListeners();
  // }

  refreshItem(int index, int pageNum) async {
    // String tagId = channelTagsData.tag[index].tagId;
    // var result = await RtcApi.instance.getSideBarWithTagId(tagId, pageNum, pageSize, snapshotId);
    // if(result!=null && result.data!=null){
    //   sideBarMap[tagId] = result.data!;
    //   snapshotId = result.data!.sidebar.snapshotId;

    //   if(pageNum == 1){
    //     itemList.clear();
    //     itemList.addAll(result.data!.sidebar.channelList);
    //   }
    //   else {
    //     itemList.addAll(result.data!.sidebar.channelList);
    //   }

    //   notifyListeners();
    // }
  }

  selectItemWithIndex(int index) {
    if (isAnimating) {
      if (index == currentTabIndex) {
        isAnimating = false;
      }
    } else {
      currentTabIndex = index;
      notifyListeners();
    }
  }

  toChatRoom(BuildContext context) {
    // Navigator.of(context).push(CupertinoPageRoute(builder: (context){
    //   return getChatRoomDemo();
    // }));
  }

  @override
  observeMessage(int messageId, data) {
    // TODO: implement observeMessage
  }

  // onItemPressed(BuildContext context,Channel item){
  //   /// 调用推荐房间接口
  //   RtcApi.instance.recommendRoom(item.channelId.toString());
  // }

  /// 跳转到指定房间
  // jumpRoom(BuildContext context,Channel item){
  //   jumpToHallPage(
  //     context,
  //     item.channelId,
  //     item.hasPassword,
  //     item.owner.id.toInt() ==
  //         (UserManager.instance.currentUser?.id.toInt() ?? -1),
  //     joinSource: JoinSource.ROOM_LIST,
  //   );
  // }

  /// 上拉刷新
  Future<void> refreshData() async {
    // pageNum = 1;

    // String tagId = channelTagsData.tag[currentTabIndex].tagId;
    // var result = await RtcApi.instance.getSideBarWithTagId(
    //   tagId, pageNum, pageSize, snapshotId,);

    // if(!result.isSuccess){
    //   showToast("${result.errorCode}");
    //   return;
    // }
    // /// 清除数据
    // sideBarMap.clear();
    // itemList.clear();
    // snapshotId = '';

    // sideBarMap[tagId] = result.data!;
    // snapshotId = result.data!.sidebar.snapshotId;

    // var tempList = result.data!.sidebar.channelList;
    // itemList.addAll(tempList);

    // if (itemList.length < pageSize) {
    //   canLoadMore = false;
    // }

    // refreshController.refreshCompleted();

    // notifyListeners();
  }

  /// 加载更多
  Future<void> loadMore() async {
    if (!canLoadMore) {
      return;
    }
    // pageNum++;

    // String tagId = channelTagsData.tag[currentTabIndex].tagId;
    // var result = await RtcApi.instance.getSideBarWithTagId(
    //   tagId, pageNum, pageSize, snapshotId,);
    // if (!result.isSuccess) {
    //   showToast("${result.errorCode}");
    //   return;
    // }

    // if (result != null && result.data != null) {
    //   sideBarMap[tagId] = result.data!;
    //   snapshotId = result.data!.sidebar.snapshotId;

    //   var tempList = result.data!.sidebar.channelList;
    //   itemList.addAll(tempList);

    //   if (tempList.length == pageSize) {
    //     canLoadMore = true;
    //   } else {
    //     canLoadMore = false;
    //   }

    //   refreshController.loadComplete();
    // }
    notifyListeners();
  }
}
